<template>
  <div>
    <el-row>
    <el-col class="image" :span="12" :xs="12" :sm="12" :md="12" :lg="12" :xl="12" >
        <img src="../../assets/image/图片1.webp" alt="">
    </el-col>
    <el-col :span="6" :offset="3"  :xs="6" :sm="6" :md="6" :lg="6" :xl="6" >
       <div class="login-wrapped">
        <el-form class="form" v-if="showLogin" :model="formModel" ref="form" :rules="rules">
            <el-form-item class="title">
                <h3>欢迎登录!</h3>
            </el-form-item>
            <el-form-item prop="username">
              <el-input  size="large" placeholder="请输入账号" v-model="formModel.username" ></el-input>
            </el-form-item>
            <el-form-item prop="passwword">
                <el-input type="password" size="large" placeholder="请输入密码" v-model="formModel.password" show-password></el-input>    
            </el-form-item>
            <el-form-item >
                <div class="forget" >
                    <el-checkbox  value="记住密码">记住密码</el-checkbox>
                    <el-link >忘记密码？</el-link>
                </div>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="Login">登录</el-button>
            </el-form-item>
            <el-form-item>
                <el-link @click="showLogin = false">去注册</el-link>
            </el-form-item>
        </el-form>

        <!-- 注册页面 -->
        <el-form class="form" :model="formModel" :rules="rules" ref="form" v-else>
            <el-form-item class="title">
                <h3>欢迎注册!</h3>
            </el-form-item>
            <el-form-item>
              <el-input  size="large" placeholder="请输入账号"  v-model="formModel.username"></el-input>
            </el-form-item>
            <el-form-item>
                <el-input type="password" size="large" placeholder="请输入密码" v-model="formModel.password" show-password></el-input>    
            </el-form-item>
            <el-form-item>
                <el-input type="password" size="large" placeholder="再次请输入密码" v-model="formModel.repassword" show-password></el-input>    
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="Register">注册</el-button>
            </el-form-item>
            <el-form-item>
                <el-link @click="showLogin=true" >去登录</el-link>
            </el-form-item>
        </el-form>
       </div>
    </el-col>
  </el-row>
  </div>
</template>

<script setup>
import {ref} from 'vue'
import { loginSeriver,registerSeriver} from '@/api/login'
import {useUserStore } from '@/stores/modules/user'
import { ElMessage } from 'element-plus';
import {useRouter } from 'vue-router'
const showLogin  = ref(true)
const userStore = useUserStore()
const router = useRouter()
const form = ref()
const formModel = ref({
    username:'',
    password:'',
    repassword:'',
})
// 规则
const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 12, message: '用户名必须是 5-10位 的字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    {
      pattern: /^\S{6,15}$/,
      message: '密码必须是 6-15位 的非空字符',
      trigger: 'blur'
    }
  ],
  repassword: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    {
      pattern: /^\S{6,15}$/,
      message: '密码必须是 6-15位 的非空字符',
      trigger: 'blur'
    },
    {
      validator: (rule, value, callback) => {
        // 判断 value 和 当前 form 中收集的 password 是否一致
        if (value !== formModel.value.password) {
          callback(new Error('两次输入密码不一致'))
        } else {
          callback() // 就算校验成功，也需要callback
        }
      },
      trigger: 'blur'
    }
  ]
}
// 登录
const Login = async()=>{
  await form.value.validate()
  const res = await loginSeriver(formModel.value)
  console.log('token：',res.data.token)
  
  userStore.setToken(res.data.token)
  ElMessage.success('登录成功')
  router.push('/')
}
// 注册
const Register = async()=>{
 // 注册成功之前，先进行校验，校验成功 → 请求，校验失败 → 自动提示
  await form.value.validate()
  await registerSeriver(formModel.value)
  ElMessage.success('注册成功')
  showLogin.value = true
}
</script>

<style lang="scss" scoped>
.el-row{
    height: 100vh;
    overflow: hidden;
}
.image{
 
    img{
        width: 100%;
        height: 100vh;
       
    }
}
.login-wrapped{
    .form{
     width: 80%;
     height: 400px;
     margin: 40% auto;
     display: flex;
     flex-direction: column;
     justify-content: space-around;
     .title{
        margin: 20px 0;
        h3{
            font-size: 25px;
        }
        
    }
    .forget{
        width: 100%;
        display: flex;
        justify-content: space-between;
        align-items: center;
        font-size: 16px;
    }
    .el-button{
        width: 100%;
    }
    }
   
   
}
</style>